﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;

namespace PixelDetectSDKInterface
{
    public enum HandleResult : int
    {
        Unknow = 1,
        OK = 0,    
        LoadImageFailed = -1,
        AdjustFailed = -2,
        DetectFailed = -3,
        SaveFailed = -4,
        AssessFailed = -5,
        AccessStringFailed = -6,
        InitFailed = -7,
        LoadStdFailed = -8,
        DetectedNoFace = -100,
        AssessNoPass = -200,
        UndefineError = -99,
    }

    public class DetectHandleResult
    {
        private HandleResult _result = HandleResult.Unknow;
        public HandleResult Result
        {
            get { return _result; }
        }

        private string _error = "";
        public string ErrorMessage
        {
            get { return _error; }
        }

        private string _accessString = "";
        public string AccessString
        {
            get { return _accessString; }
            set { _accessString = value; }
        }

        private _FGDetectAssess _detectAssess = new _FGDetectAssess();
        public _FGDetectAssess DetectAccess
        {
            get { return _detectAssess; }
            set
            {
                _detectAssess = value; 
                if(value.pass > 0)
                {
                    _result = HandleResult.OK;
                }
                else
                {
                    _result = HandleResult.AssessNoPass;
                }
            }
        }

        public void SetWellDone()
        {
            _result = HandleResult.OK;
            _error = "";
            _accessString = "";
        }

        public void Reset()
        {
            _result = HandleResult.Unknow;
            _error = "";
            _accessString = "";
            _detectAssess = new _FGDetectAssess();
        }

        public void TakeException(Exception ex)
        {
            StackFrame frame = new StackFrame(1);
            MethodBase method = frame.GetMethod();
            string methodName = method.Name.ToUpper();
            switch (methodName)
            {
                case "LOADSTD":
                    _result = HandleResult.LoadStdFailed;
                    _error = "加载标准文件发生了异常:" + ex.Message;
                    break;
                case "INIT":
                    _result = HandleResult.InitFailed;
                    _error = "SDK初始化发生了异常:" + ex.Message;
                    break;
                case "LOAD":
                    _result = HandleResult.LoadImageFailed;
                    _error = "加载图像数据发生了异常:" + ex.Message; 
                    break;
                case "DETECT":
                    _result = HandleResult.DetectFailed;
                    _error = "检测图像发生了异常:" + ex.Message;
                    break;
                case "ADJUST":
                    _result = HandleResult.AdjustFailed;
                    _error = "调整图像发生了异常:" + ex.Message;
                    break;
                case "SAVE":
                    _result = HandleResult.SaveFailed;
                    _error = "保存图像数据发生了异常:" + ex.Message;
                    break;
                case "ASSESS":
                    _result = HandleResult.AssessFailed;
                    _error = "评价照片质量发生了异常:" + ex.Message;
                    break;
                case "ASSESSSTRING":
                    _result = HandleResult.AccessStringFailed;
                    _error = "获取照片质量评价发生了异常:" + ex.Message;
                    break;
                default:
                    break;
            }
        }

        public void TakeError(int ret)
        {
            if(ret == 0)
            {
                return;
            }
            StackFrame frame = new StackFrame(1);
            MethodBase method = frame.GetMethod();
            string methodName = method.Name.ToUpper();
            switch(methodName)
            {
                case "LOADSTD":
                    _result = HandleResult.LoadStdFailed;
                    _error = "加载标准文件失败:" + ParseErrorMessage(ret);
                    break;
                case "INIT":
                    _result = HandleResult.InitFailed;
                    _error = "SDK初始化失败:" + ParseErrorMessage(ret);
                    break;
                case "LOAD":
                    _result = HandleResult.LoadImageFailed;
                    _error = "加载图像数据失败:" + ParseErrorMessage(ret);
                    break;
                case "DETECT":
                    if(CheckFaceNotExists(ret))
                    {
                        _result = HandleResult.DetectedNoFace;
                    }
                    else
                    {
                        _result = HandleResult.DetectFailed;
                    }
                    _error = "检测图像失败:" + ParseErrorMessage(ret);
                    break;
                case "ADJUST":
                    if (CheckFaceNotExists(ret))
                    {
                        _result = HandleResult.DetectedNoFace;
                    }
                    else
                    {
                        _result = HandleResult.AdjustFailed;
                    }                   
                    _error = ParseErrorMessage(ret);
                    break;
                case "SAVE":
                    _result = HandleResult.SaveFailed;
                    _error = "保存图像数据失败:" + ParseErrorMessage(ret);
                    break;
                case "ASSESS":
                    _result = HandleResult.AssessFailed;
                    _error = "评价照片质量失败:" + ParseErrorMessage(ret);
                    break;
                case "ASSESSSTRING":
                    _result = HandleResult.AccessStringFailed;
                    _error = "获取照片质量评价失败:" + ParseErrorMessage(ret);
                    break;
                default:
                    break;
            }
        }

        private bool CheckFaceNotExists(int code)
        {
            PixelError errorType = (PixelError)code;
            switch (errorType)
            {
                case PixelError.PIXEL_IMAGECROPSCALELACK:
                case PixelError.PIXEL_NOFACE:
                case PixelError.PIXEL_MULTIFACE:
                case PixelError.PIXEL_NO_KEY_POINTS:
                case PixelError.PIXEL_CREATETEMPLATE:
                    return true;
                default:
                    return false;
            }
        }

        public static string ParseErrorMessage(int code)
        {
            PixelError errorType = (PixelError)code;
            switch (errorType)
            {
                case PixelError.PIXEL_SUCCESS:
                    return "";
                case PixelError.PIXEL_FAILURE:
                    return code.ToString() + ", 一般性错误";
                case PixelError.PIXEL_NO_MEMORY:
                    return code.ToString() + ", 内存不足";
                case PixelError.PIXEL_FILENOTFOUND:
                    return code.ToString() + ", 找不到文件";
                case PixelError.PIXEL_FILE_READ_ERROR:
                    return code.ToString() + ", 读取文件出错";
                case PixelError.PIXEL_INV_HANDLE:
                    return code.ToString() + ", 无效句柄";
                case PixelError.PIXEL_INV_PARAMETER:
                    return code.ToString() + ", 无效参数";
                case PixelError.PIXEL_IMAGE_NULL:
                    return code.ToString() + ", 照片数据为空";
                case PixelError.PIXEL_IMAGEFORMAT_UNKNOW:
                    return code.ToString() + ", 不支持的图片格式";
                case PixelError.PIXEL_UNSUPPORT:
                    return code.ToString() + ", 不支持的接口";
                case PixelError.PIXEL_MODELNOTFOUND:
                    return code.ToString() + ", 未加载模型";
                case PixelError.PIXEL_DEVICE_UNSUPPORT:
                    return code.ToString() + ", 找不到可用设备";
                case PixelError.PIXEL_INV_DEVICE_ID:
                    return code.ToString() + ", 无效的设备id";
                case PixelError.PIXEL_STDDATA_NOTFOUND:
                    return code.ToString() + ", 没加载图像检测标准";
                case PixelError.PIXEL_UNKNOW_STDDATA:
                    return code.ToString() + ", 不支持的图像检测标准";
                case PixelError.PIXEL_IMAGECOLORS:
                    return code.ToString() + ", 照片不是24位格式";
                case PixelError.PIXEL_IMAGECROPSCALELACK:
                    return code.ToString() + ", 原图裁切比例不足";
                case PixelError.PIXEL_NOFACE:
                    return code.ToString() + ", 检测不到人脸";
                case PixelError.PIXEL_MULTIFACE:
                    return code.ToString() + ", 多张人脸";
                case PixelError.PIXEL_NO_KEY_POINTS:
                    return code.ToString() + ", 检测不到人脸关键点";
                case PixelError.PIXEL_CREATETEMPLATE:
                    return code.ToString() + ", 抽取人脸特征失败";
                case PixelError.PIXEL_READ_IDCARD_ERROR:
                    return code.ToString() + ", 读取身份证失败";
                case PixelError.PIXEL_LICENSE_MISMATCH:
                    return code.ToString() + ", 读取授权文件失败";
                default:
                    return code.ToString() + ", 未定义的错误"; ;
            }
        }
    }
}
